﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Maintanence_Printing_Tool
{
    class dbFunctions
    {
        public static string connectionstring = ConfigurationManager.AppSettings["ConStr"];
       
        public static DataTable getTable(string sqlQuery)
        {
            DataTable dt = new DataTable();
            try
            {

                SqlConnection con = new SqlConnection(connectionstring);
                SqlCommand cmd = new SqlCommand(sqlQuery, con);
                cmd.Connection.Open();
                SqlDataAdapter sqlDA = new SqlDataAdapter(cmd);
                sqlDA.Fill(dt);
            }
            catch (Exception ex)
            {
             
            }
            return dt;
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
               // MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

        public static bool InsertInward(string UniqueBarcodeId, float weight)
        {
            bool isInwardSuccess = false;
            SqlConnection objCon = new SqlConnection(connectionstring);
            try
            {
                SqlCommand objCmd = new SqlCommand();
                objCmd.Connection = objCon;

                objCon.Open();

                string InsertSQL = "pr_InsertInward " + UniqueBarcodeId + ","  + weight;
                objCmd.CommandText = InsertSQL;
                objCmd.ExecuteNonQuery();
                isInwardSuccess = true;
            }
            catch (Exception Ex)
            {
                isInwardSuccess = false;
            }
            finally
            {
                objCon.Close();
            }
            return isInwardSuccess;
        }
        public static bool InsertWIPInward(string UniqueBarcodeId, float weight)
        {
            bool isInwardSuccess = false;
            SqlConnection objCon = new SqlConnection(connectionstring);
            try
            {
                SqlCommand objCmd = new SqlCommand();
                objCmd.Connection = objCon;

                objCon.Open();

                string InsertSQL = "pr_InsertWIPInward " + UniqueBarcodeId + "," + weight;
                objCmd.CommandText = InsertSQL;
                objCmd.ExecuteNonQuery();
                isInwardSuccess = true;
            }
            catch (Exception Ex)
            {
                isInwardSuccess = false;
            }
            finally
            {
                objCon.Close();
            }
            return isInwardSuccess;
        }
        public static bool InsertFGInward(string UniqueBarcodeId, int ProductName, int weight)
        {
            bool isInwardSuccess = false;
            SqlConnection objCon = new SqlConnection(connectionstring);
            try
            {
                SqlCommand objCmd = new SqlCommand();
                objCmd.Connection = objCon;

                objCon.Open();

                string InsertSQL = "pr_InsertFGInward " + UniqueBarcodeId + "," + ProductName + "," + weight;
                objCmd.CommandText = InsertSQL;
                objCmd.ExecuteNonQuery();
                isInwardSuccess = true;
            }
            catch (Exception Ex)
            {
                isInwardSuccess = false;
            }
            finally
            {
                objCon.Close();
            }
            return isInwardSuccess;
        }

        public static bool InsertWIPInward(string UniqueBarcodeId, string ProductName, int weight)
        {
            bool isInwardSuccess = false;
            SqlConnection objCon = new SqlConnection(connectionstring);
            try
            {
                SqlCommand objCmd = new SqlCommand();
                objCmd.Connection = objCon;

                objCon.Open();

                string InsertSQL = "pr_Insert_WIP_Inward " + UniqueBarcodeId + "," + "'" + ProductName + "'" + "," + weight;
                objCmd.CommandText = InsertSQL;
                objCmd.ExecuteNonQuery();
                isInwardSuccess = true;
            }
            catch (Exception Ex)
            {
                isInwardSuccess = false;
            }
            finally
            {
                objCon.Close();
            }
            return isInwardSuccess;
        }

        public static bool InsertManualInward(string UniqueBarcodeId, string BarCodeId, double InwardQty,string InvoiceNo,int SupplierId)
        {
            bool isInwardSuccess = false;
            SqlConnection objCon = new SqlConnection(connectionstring);
            try
            {
                SqlCommand objCmd = new SqlCommand();
                objCmd.Connection = objCon;

                objCon.Open();

                string InsertSQL = "pr_mt_InsertManualInward_Su_Id " + UniqueBarcodeId + "," + BarCodeId + "," + InwardQty + "," + "'" + InvoiceNo + "'" + "," + SupplierId;
                objCmd.CommandText = InsertSQL;
                objCmd.ExecuteNonQuery();
                isInwardSuccess = true;
            }
            catch (Exception Ex)
            {
                isInwardSuccess = false;
            }
            finally
            {
                objCon.Close();
            }
            return isInwardSuccess;
        }
        public static bool InsertManualOutward(string UniqueBarcodeId, string BarCodeId, double InwardQty)
        {
            bool isOutwardSuccess = false;
            SqlConnection objCon = new SqlConnection(connectionstring);
            try
            {
                SqlCommand objCmd = new SqlCommand();
                objCmd.Connection = objCon;

                objCon.Open();

                string InsertSQL = "pr_mt_InsertManualOutward_Su_Id " + UniqueBarcodeId + "," + BarCodeId + "," + InwardQty ;
                objCmd.CommandText = InsertSQL;
                objCmd.ExecuteNonQuery();
                isOutwardSuccess = true;
            }
            catch (Exception Ex)
            {
                isOutwardSuccess = false;
            }
            finally
            {
                objCon.Close();
            }
            return isOutwardSuccess;
        }
        public static bool InsertMachineOutward(string BarCodeId,int MachineId, string MachineName,string Location,int shift,string Reason,int Operatord,int DepartmentId,double Quantity)
        {
            bool isOutwardMachineSuccess = false;
            SqlConnection objCon = new SqlConnection(connectionstring);
            try
            {
                SqlCommand objCmd = new SqlCommand();
                objCmd.Connection = objCon;

                objCon.Open();

                string InsertSQL = "pr_mt_InsertMachneLifeTime '" + BarCodeId + "'" + "," + MachineId + "," + "'" + MachineName + "'" + "," + "'" + Location + "'" + "," + shift + "," + "'" + Reason + "'" + "," + Operatord + "," + DepartmentId + "," + Quantity;
                objCmd.CommandText = InsertSQL;
                objCmd.ExecuteNonQuery();
                isOutwardMachineSuccess = true;
            }
            catch (Exception Ex)
            {
                isOutwardMachineSuccess = false;
            }
            finally
            {
                objCon.Close();
            }
            return isOutwardMachineSuccess;
        }
        public static bool InsertOutward(string UniqueBarcodeId, double OutwardQty)
        {
            bool isOutwardSuccess = false;
            SqlConnection objCon = new SqlConnection(connectionstring);
            try
            {
                SqlCommand objCmd = new SqlCommand();
                objCmd.Connection = objCon;

                objCon.Open();

                string InsertSQL = "pr_InsertOutward " + UniqueBarcodeId + "," + OutwardQty;
                objCmd.CommandText = InsertSQL;
                objCmd.ExecuteNonQuery();
                isOutwardSuccess = true;
            }
            catch (Exception Ex)
            {
                isOutwardSuccess = false;
            }
            finally
            {
            }
            return isOutwardSuccess;
        }
        
    }
}
